package com.corner.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.corner.R;
import com.corner.app.ConsTants;
import com.corner.model.db.Session;
import com.corner.presenter.imp.SessionPresenter;
import com.corner.ui.activity.MainActivity;
import com.corner.ui.activity.NotifyActivity;
import com.corner.ui.adapter.SessionAdapter;
import com.corner.ui.base.BaseFragment;
import com.corner.ui.view.ISessionView;
import com.corner.utils.TypeFaceUtil;
import com.corner.utils.view.FindView;
import com.corner.utils.view.widget.XListView;
import com.daimajia.swipe.SwipeLayout;

import java.util.List;

/**
 * 会话Fragment
 */
public class SessionFragment extends BaseFragment<SessionPresenter> implements View.OnClickListener, ISessionView {
    @FindView(R.id.lv_chat_friend_list)
    private XListView lvSession;

    /**
     * 通知的headerview
     */
    private View notifyHeader;
    private View headerDivider;
    private SessionAdapter adapter;

    @Override
    public void onResume() {
        super.onResume();
        closeSwipe(-1);
    }

    @Override
    protected void setOnclick() {
        lvSession.addHeaderView(notifyHeader);
        lvSession.addHeaderView(headerDivider);
        notifyHeader.setOnClickListener(this);
        lvSession.setAutoLoadEnable(true);
        lvSession.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int) event.getX();
                int y = (int) event.getY();
                int position = lvSession.pointToPosition(x, y);
                closeSwipe(position);
                return false;
            }
        });

        lvSession.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                presenter.refresh();
            }

            @Override
            public void onLoadMore() {
                presenter.loadMore();
            }
        });
        initData();
    }

    /**
     * 关闭滑动
     *
     * @param position
     */
    public void closeSwipe(int position) {
        for (int i = 0; i < lvSession.getChildCount(); i++) {
            if (position != i) {
                View child = lvSession.getChildAt(i).findViewById(R.id.swipe_frag_session);
                if (child != null) {
                    ((SwipeLayout) child).close(true);
                }
            }
        }
    }

    /**
     * 初始化数据
     */
    private void initData() {
        presenter.refresh();
//        presenter.checkNotReadedSession();
        presenter.checkNotify();
    }

    @Override
    protected SessionPresenter createP() {
        return new SessionPresenter();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected View initView(LayoutInflater inflater) {
        View view = View.inflate(getActivity(), R.layout.frag_session, null);
        notifyHeader = View.inflate(getActivity(), R.layout.lv_session_notify_headder, null);
        TextView title = (TextView) notifyHeader.findViewById(R.id.tv_frag_session_header_title);
        TypeFaceUtil.setTypeFace(activity, title, ConsTants.TYPE_FACE);
        headerDivider = View.inflate(getActivity(), R.layout.lv_session_notify_header_divider, null);
        TypeFaceUtil.setTypeFace(activity, (TextView) headerDivider, ConsTants.TYPE_FACE);
        return view;
    }

    @Override
    public void onClick(View v) {
        if (v == notifyHeader) {
            Intent intent = new Intent(getActivity(), NotifyActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void showRefreshSession(List<Session> sessions) {
        if (adapter == null) {
            adapter = new SessionAdapter(sessions, activity);
            lvSession.setAdapter(adapter);
        } else {
            adapter.changeList(sessions);
        }
        closeSwipe(-1);
    }

    @Override
    public void showLoadMoreSession(List<Session> sessions) {
        adapter.changeList(sessions);
        closeSwipe(-1);
    }

    @Override
    public void refreshComplete() {
        lvSession.stopRefresh();
    }

    @Override
    public void loadMoreComplete() {
        lvSession.stopLoadMore();
    }

    @Override
    public void showHasNotify(boolean hasNotify) {
        ((MainActivity) activity).setMessageNotReaded(hasNotify);
        if (hasNotify)
            notifyHeader.findViewById(R.id.iv_frag_session_readed).setVisibility(View.VISIBLE);
        else
            notifyHeader.findViewById(R.id.iv_frag_session_readed).setVisibility(View.INVISIBLE);
    }

    @Override
    public void showHasNotReadedSession(boolean hasNotReaded) {
        ((MainActivity) activity).setMessageNotReaded(hasNotReaded);
    }

    @Override
    public void setLoadMoreEnable(boolean enable) {
        lvSession.setPullLoadEnable(enable);
    }
}
